import {_decorator, Component, Label, Node, Sprite} from 'cc';
import {SkillItemController} from "db://assets/scripts/ui/skill/SkillItemController";
import {AudioManager} from "db://assets/scripts/common/AudioManager";
import {Utils} from "db://assets/scripts/utils/Utils";
import {GameManager} from "db://assets/scripts/game/GameManager";
import {CSVManager} from "db://assets/scripts/common/CSVManager";
import {UIManager} from "db://assets/scripts/common/UIManager";
import {PlayerDataManager} from "db://assets/scripts/common/PlayerDataManager";
import {AudioPaths} from "db://assets/scripts/common/AudioPaths";
import {Skill} from "db://assets/scripts/data/model/Skill";
import {SkillConverter} from "db://assets/scripts/data/converter/SkillConverter";
import {PlayerSkillManager} from "db://assets/scripts/game/role/player/PlayerSkillManager";

const {ccclass, property} = _decorator;

/**
 * 技能商店
 */
@ccclass('SkillPanelController')
export class SkillPanelController extends Component {
    /**
     * 技能列表
     */
    @property(Node)
    public skillsNode: Node;
    /**
     * 金币
     */
    @property(Label)
    public goldLabel: Label;
    /**
     * 刷新按钮
     */
    @property(Sprite)
    public refreshIconSprite: Sprite;

    /**
     * 如果放弃获得技能的机会，那么将可获得50金币
     */
    private gold: number = 50;
    /**
     * 回调
     */
    private callback: Function;

    /**
     * 初始化
     *
     * @param callback 回调
     */
    public init(callback?: Function) {
        this.callback = callback;
    }

    /**
     * 展示技能
     */
    public show() {
        this.goldLabel.string = `获得 ${this.gold}`;

        // 获取玩家所有未解锁技能
        let lockSkillIds: string[] = PlayerSkillManager.getInstance().getLockPlayerSkill();
        // 打乱
        lockSkillIds = Utils.shuffle(lockSkillIds);

        // 所有玩家可拥有的技能
        let playerSkillCSV = CSVManager.getInstance().getTable("playerSkill");
        // 转换数据
        let playerSkills = SkillConverter.convertFromCSV(playerSkillCSV);

        // 在技能列表上展示这些技能
        this.skillsNode.children.forEach((child: Node, idx: number) => {
            if (lockSkillIds[idx]) {
                let playerSkill: Skill = playerSkills.find(value => value.id === lockSkillIds[idx]);
                let skillItemController = child.getComponent(SkillItemController);
                skillItemController.init(playerSkill, () => {
                    this.onClose();
                });
                // 激活节点
                child.active = true;
            } else {
                // 注销节点
                child.active = false;
            }
        })
    }

    /**
     * 放弃选择技能的机会，选择获得金币
     */
    public onCancel() {
        // 播放声音
        AudioManager.getInstance().playSound(AudioPaths.sellSound);
        // 获得金币
        GameManager.getInstance().gameGoldProxy.addGold(this.gold);
        // 关闭技能商店
        this.onClose();
    }

    /**
     * 刷新技能商店
     */
    public onRefresh() {
        // 播放声音
        AudioManager.getInstance().playSound(AudioPaths.clickSound);

        // 刷新技能列表
        this.show();
    }

    /**
     * 关闭技能商店
     */
    public onClose() {
        // 隐藏技能商店界面
        UIManager.getInstance().hideDialog("skill/skillPanel");
        // 显示战斗界面
        UIManager.getInstance().showDialog("fight/fightPanel");
        // 回调
        this.callback && this.callback();
    }
}